package chat.user.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import chat.user.domain.User;
import chat.user.service.UserException;
import chat.user.service.UserService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * Servlet implementation class OnlineServlet
 */
@WebServlet("/OnlineServlet")
public class OnlineServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	UserService userService = new UserService();
  

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("content-type","text/html;charset=UTF-8");
		String method = request.getParameter("method");
		
		
		if(method.equals("allUser"))
		{
			List<User> userList = userService.findAll();
			JSONArray ja = JSONArray.fromObject(userList);
			
			response.getWriter().println(ja.toString());
			
		}
		if(method.equals("online"))
		{
			List<User> userList = userService.findOnline();
			JSONArray ja = JSONArray.fromObject(userList);
			
			response.getWriter().println(ja.toString());
		}
		if(method.equals("onliner"))
		{
			int num = userService.findNumber();
			JSONArray ja = JSONArray.fromObject(num);
			
			response.getWriter().println(ja.toString());
		}
//		if(method.equals("lastupdatetime"))
//		{
//			User user = (User)request.getSession().getAttribute("session_user");
//			userService.updatelasttime(user.getId());
//			
//		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("content-type","text/html;charset=UTF-8");
		
		
	}
}
